package com.dashan.backend.datastore;

import javax.jdo.JDODataStoreException;
import javax.jdo.JDOObjectNotFoundException;

import com.dashan.backend.DashanException;

public class UserDao extends DaoBase {
    
    public DsUser createUser(String userName, String nickName, String pwdSha) 
    throws JDODataStoreException, DashanException {
        if (isUserExist(userName)) {
            throw new DashanException(String.format("User \"%s\" already exist.", userName));
        }
        DsUser user = new DsUser(userName, nickName, pwdSha);
        pm.makePersistent(user);
        return user;
    }
    
    public DsUser getUser(String userName) throws JDOObjectNotFoundException {
        DsUser user = pm.getObjectById(DsUser.class, userName);
        return user;
    }
    
    public boolean isUserExist(String userName) {
        try {
            pm.getObjectById(DsUser.class, userName);
        } catch (JDOObjectNotFoundException e) {
            return false;
        }
        return true;
    }
}
